package com.chris.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.chris.bean.User;
import com.chris.dao.BaseDAO;

public class UserDAOImpl implements BaseDAO {
	private SessionFactory sessionFactory;

	public User findByUserId(int id) {
		return (User) getSession().get(User.class, id);
	}
	
	public List<User> findAllUser(String tj) throws Exception {
		String sql = "select * from user";
		Query query = getSession().createSQLQuery(sql).addEntity(User.class);
		return query.list();
	}
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int update(Object obj) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(Object obj) throws Exception {
		getSession().save((User)obj);
		return 0;
	}

	@Override
	public int delete(Object obj) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object findById(int id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int count(String tj) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Object> findAll(String tj) throws Exception {
		String sql = "select * form User";
		Query query = getSession().createQuery(sql);
		return query.list();
	}
}
